/**
 * Description:
 * Class Name:CrazyitObjectFactory
 * Date:2022/5/1 13:42
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第18章_类加载机制与反射._18_6_反射和泛型._18_6_1_泛型和Class类;

import javax.swing.*;
import java.util.Date;

public class CrazyItObjectFactory {
    public static Object getInstance(String clsName){
        try {
            // 创建指定类对应的Class对象
            Class cls = Class.forName(clsName);
            // 返回使用该Class对象创建的实例
            return cls.getConstructor().newInstance();
        }
        catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        Date d = (Date)CrazyItObjectFactory.getInstance("java.util.Date");
        JFrame f = (JFrame)CrazyItObjectFactory.getInstance("java.util.Date");
    }
}
